package com.springcloud.sc.ucpv5.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springcloud.sc.comm.core.constant.CacheConstants;
import com.springcloud.sc.comm.core.constant.SecurityConstants;
import com.springcloud.sc.comm.core.util.R;
import com.springcloud.sc.comm.security.annotation.Inner;
import com.springcloud.sc.comm.security.service.ScUser;
import com.springcloud.sc.comm.security.util.SecurityUtils;
import com.springcloud.sc.ucp.v5.api.feign.UcpRemoteUserService;
import com.springcloud.sc.ucp.v5.api.vo.GroupDO;
import com.springcloud.sc.ucp.v5.api.vo.NodeDO;
import com.springcloud.sc.ucp.v5.api.vo.UserInfoDO;
import com.springcloud.sc.ucp.v5.api.vo.UserVO;
import com.springcloud.sc.ucpv5.service.UcpService;
import com.springcloud.sc.ucpv5.tree.data.WebTreeNode;
import com.springcloud.sc.upms.api.dto.UserInfo;
import com.springcloud.sc.upms.api.entity.SysUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/userInfo")
public class UcpController {

    @Autowired
    private UcpService ucpService;

    @Autowired
    private UcpRemoteUserService ucpRemoteUserService;

    //显示分页数据
    @RequestMapping("/findPage")
    public Page findPage(@RequestParam Map<String, Object> params) {
//        String uid =  this.getUserId();
        String uid =  null;
        String userId =String.valueOf(uid);
        Page page =  ucpService.findListById( params , userId );
        return page;
    }

    @Inner(value = false)
    @ApiOperation(value = "获取用户树", notes = "根据当前登录用户")
    @GetMapping(value = "/getTreeByNodeId")
    public String getTreeByNodeId() {
//        String uid =  this.getUserId();
        String uid =  null;
        String userId =String.valueOf(uid);
        WebTreeNode webTreeNode  = ucpService.getWebListTree(userId);
        String jsonStr = JSONObject.toJSONString( webTreeNode );
        //类似于以数组的方式来返回
        return "["+jsonStr+"]";
    }

    @Inner(value = false)
    @ApiOperation(value = "获取用户树", notes = "获取用户树")
    @GetMapping(value = "/tree")
    public String getWebTree() {

        ScUser scUser= SecurityUtils.getUser();
        String ucpId =scUser.getUcpId();

        WebTreeNode webTreeNode  = ucpService.getWebListTree( ucpId );
        String jsonStr = JSONObject.toJSONString( webTreeNode );
        //类似于以数组的方式来返回
        return "["+jsonStr+"]";
    }

    @ApiOperation(value = "获取用户树", notes = "只获取当前节点层级上的数据")
    @GetMapping(value = "/limitTree")
    public String getLimitWebTree(@RequestParam Map<String, Object> params){
//        String uid =  this.getUserId();
        String uid =  null;
        String userId =String.valueOf(uid);
        List list  = ucpService.getNodeTree(params , userId);
        String jsonStr = JSONObject.toJSONString( list );
        //类似于以数组的方式来返回
        System.out.println(jsonStr);
        return jsonStr;
    }

    @Inner(value = false)
    @ApiOperation(value = "获取用户树", notes = "根据组获取用户树,对外提供的相应接口")
    @GetMapping("/getUserInfoList/{groupNumber}")
    public String getUserInfoList(@PathVariable String groupNumber){
        if(groupNumber == null || groupNumber.equals("null") || groupNumber.equals("")){
            return null;
        }else{
            return ucpService.getUserInfoListTreeByGroupNumber( groupNumber );
        }
    }

    @ApiOperation(value = "获取用户列表", notes = "根据组获取用户列表")
    @GetMapping("/getUserListByGroupNumber/{groupNumber}")
    public List getUserListByGroupNumber(@PathVariable String groupNumber){
        if(groupNumber == null || groupNumber.equals("null") || groupNumber.equals("")){
            return null;
        }else{
            return ucpService.getUserListByGroupNumber(groupNumber);
        }
    }

    @ApiOperation(value = "获取用户列表", notes = "根据用户号获取让所在组用户列表")
    @GetMapping("/getUserListByUserNumber/{userNumber}")
    public  List getUserListByUserNumber(@PathVariable String userNumber){
        if(userNumber == null || userNumber.equals("null") || userNumber.equals("")){
            return null;
        }else{
            return ucpService.getUserListByUserNumber(userNumber);
        }
    }

    @RequestMapping("/getNode")
    public NodeDO getNode(@RequestParam Map<String, Object> params ){
        NodeDO nodeDO = ucpService.getNode( params );
        return nodeDO;
    }
    @RequestMapping("/getGroup")
    public GroupDO getGroup(@RequestParam Map<String, Object> params ){
        GroupDO groupDO = ucpService.getGroup( params );
        return groupDO;
    }
    @RequestMapping("/getUser")
    public UserInfoDO getUser(@RequestParam Map<String, Object> params ){
        UserInfoDO userInfoDO = ucpService.getUser( params );
        return userInfoDO;
    }
    @PostMapping(value = "/saveUCPUser")
    public void saveUCPUser(@RequestBody UserInfoDO userInfo) {
        ucpService.saveUserInfo( userInfo );
    }

    @PostMapping("/updateUser")
    public boolean updateNode( @RequestBody UserInfoDO userInfo  ){
      return  ucpService.updateUserInfo( userInfo );
    }

    @RequestMapping("/saveNode")
    public String  saveNode(@RequestBody NodeDO nodeDO ){
        return ucpService.saveNode(nodeDO);
    }

    @PostMapping("/updateNode")
    public void updateNode( @RequestBody NodeDO nodeDO  ){
        ucpService.updateNode( nodeDO );
    }

    @PostMapping("/saveGroup")
    public void saveGroup( @RequestBody GroupDO groupDO  ){
        ucpService.saveGroup(groupDO);
    }

    @PostMapping("/updateGroup")
    public void updateGroup( @RequestBody GroupDO groupDO  ){
        ucpService.updateGroup(groupDO);
    }

    @PostMapping("/delete")
    public Boolean delete(@RequestParam Map<String, Object> params) {
//        String uid =  this.getUserId();
        String uid =  null;
        boolean bool = this.ucpService.delete( params , uid );
        return bool;
    }
}
